package com.tkjt.pms.modules.kpiadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexReport;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueAudit;
import com.tkjt.pms.common.utils.DoubleUtil;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsStringUtils;
import com.tkjt.pms.common.vos.TkIndexValueForScoreVO;
import com.tkjt.pms.modules.common.mapper.*;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.gm.service.TkGmBizService;
import com.tkjt.pms.modules.kpiadmin.mapper.KpiAdminMapper;
import com.tkjt.pms.modules.kpiadmin.model.KaiProgressDetailModel;
import com.tkjt.pms.modules.kpiadmin.model.KaiProgressViewModel;
import com.tkjt.pms.modules.kpiadmin.service.KpiAdminService;
import com.tkjt.pms.modules.kpiadmin.vo.DatagridVO;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Service
@Transactional
public class KpiAdminServiceImpl implements KpiAdminService {
    @Autowired
    private TkDataConfigService tkDataConfigService;

    @Autowired
    private Map<String, KpiDataBizServiceBase> kpiDataBizServiceMap = new HashMap<>();
    @Autowired
    private Map<String, KpiAdminBizServiceBase> kpiAdminBizServiceMap = new HashMap<>();

    @Override
    public KaiProgressViewModel getKaiProgressView(LoginUser loginUser, HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiAdminBizServiceBase kpiAdminBizServiceBase = checkKpiService(dataType);
        return kpiAdminBizServiceBase.getKaiProgressView(request,dataType);
    }

    @Override
    public KaiProgressDetailModel getKaiProgressDetail(LoginUser loginUser, HttpServletRequest request) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiAdminBizServiceBase kpiAdminBizServiceBase = checkKpiService(dataType);
        return kpiAdminBizServiceBase.getKaiProgressDetail(request,dataType);
    }

    @Override
    public List<Map<String, Object>> indexGroupConditions(LoginUser loginUser, List<Map<String, Object>> specialColums, String noVirtual,String indexYearMonth) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.indexGroupConditions(specialColums, noVirtual,dataType,indexYearMonth);
    }

    @Override
    public List<Map<String, Object>> indexGroupConditionsForChangeUser(LoginUser loginUser, List<Map<String, Object>> specialColums, String noVirtual, String indexYearMonth) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.indexGroupConditionsForChangeUser(specialColums, noVirtual,dataType,indexYearMonth);
    }

    @Override
    public List<Map<String, Object>> weightConditions(LoginUser loginUser, List<Map<String, Object>> specialColums, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.weightConditions(specialColums, noVirtual,dataType);
    }

    @Override
    public Result<?> kaTargetDatagrid(LoginUser loginUser, DatagridVO datagridVO, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaTargetDatagrid(datagridVO,noVirtual,dataType);
    }

    @Override
    public Result<?> indexValueDataSave(LoginUser loginUser, DatagridVO datagridVO) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.indexValueDataSave(datagridVO,dataType);
    }

    @Override
    public Result<?> kaFillSave(LoginUser loginUser, DatagridVO datagridVO) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaFillSave(datagridVO,dataType);
    }

    @Override
    public Result<?> kaFillDatagrid(LoginUser loginUser, DatagridVO datagridVO, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaFillDatagrid(datagridVO,noVirtual,dataType);
    }

    @Override
    public Result<?> kaMgrDatagrid(LoginUser loginUser, DatagridVO datagridVO, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaMgrDatagrid(datagridVO,noVirtual,dataType);
    }

    @Override
    public Result<?> auditDetailDatagrid(LoginUser loginUser, DatagridVO datagridVO, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.auditDetailDatagrid(datagridVO,noVirtual,dataType);
    }

    @Override
    public Result<?> kaWeightDatagrid(LoginUser loginUser, DatagridVO datagridVO, String noVirtual) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaWeightDatagrid(datagridVO,noVirtual,dataType);
    }

    @Override
    public Result<?> kaWeightSave(LoginUser loginUser, DatagridVO datagridVO) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaWeightSave(datagridVO,dataType);
    }

    @Override
    public Result<?> kaGradeDatagrid(LoginUser loginUser, TkIndexValueForScoreVO tkIndexValue, Integer pageNo, Integer pageSize) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaGradeDatagrid(tkIndexValue,pageNo,pageSize,loginUser,dataType);
    }

    @Override
    public Result<?> kaMgrAudit(LoginUser loginUser, TkIndexValueForScoreVO indexValueForScoreVO) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.kaMgrAudit(indexValueForScoreVO,dataType);
    }

    @Override
    public List<Map<String, Object>> getIndexCodeByTarget(LoginUser loginUser, String indexGroup) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        KpiDataBizServiceBase kpiDataBizServiceBase = checkDataService(dataType);
        return kpiDataBizServiceBase.getIndexCodeByTarget(indexGroup,dataType);
    }

    public KpiAdminBizServiceBase checkKpiService(String dataType) {
        String serviceName = "kpiAdminBizService" + EsStringUtils.onlyFirstCharUpper(dataType);
        return kpiAdminBizServiceMap.get(serviceName);
    }

    public KpiDataBizServiceBase checkDataService(String dataType) {
        String serviceName = "kpiDataBizService" + EsStringUtils.onlyFirstCharUpper(dataType);
        return kpiDataBizServiceMap.get(serviceName);
    }
}
